package cn.itcast.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.itcast.myapplication.bean.Student;

public class AddStudentActivity extends AppCompatActivity {
    private EditText editStudentId;
    private EditText editStudentName;
    private EditText editStudentNumber;
    private EditText editStudentUsername;
    private EditText editStudentClass;
    private Button buttonAddStudent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_student);


        editStudentId = findViewById(R.id.edit_student_id);
        editStudentName = findViewById(R.id.edit_student_name);
        editStudentNumber = findViewById(R.id.edit_student_number);
        editStudentUsername = findViewById(R.id.edit_student_username);
        editStudentClass = findViewById(R.id.edit_student_class);
        buttonAddStudent = findViewById(R.id.button_add_student);

        buttonAddStudent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = Integer.parseInt(editStudentId.getText().toString());
                String name = editStudentName.getText().toString();
                String number = editStudentNumber.getText().toString();
                String username = editStudentUsername.getText().toString();
                String className = editStudentClass.getText().toString();

                Student student = new Student(id, name, number, username, className);
                DatabaseOpenHelper dbHelper = new DatabaseOpenHelper(AddStudentActivity.this);
                dbHelper.addStudent(student);
                Toast.makeText(AddStudentActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                finish();


                // Add the new student to your data list
                // You need to implement this part according to your specific requirements
            }
        });
    }
}